package com.hschinese.pronunciation.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridView;

import com.hschinese.pronunciation.R;

/**
 * Created by zf on 2018/1/22.
 */

public class LineGridView extends GridView {
    private int rownum;
    public boolean hasScrollBar = true;
    public LineGridView(Context context) {
        super(context);
    }

    public LineGridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public LineGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if(getChildCount()>0){
            View localView1 = getChildAt(0);
            int column = getWidth() / localView1.getWidth();//计算出一共有多少列，假设有3列
            int childCount = getChildCount();//子view的总数
            System.out.println("子view的总数childCount==" + childCount);
            Paint localPaint;//画笔
            localPaint = new Paint();
            localPaint.setStyle(Paint.Style.STROKE);
            localPaint.setColor(getContext().getResources().getColor(R.color.white));//设置画笔的颜色
            for (int i = 0; i < childCount; i++) {//遍历子view
                View cellView = getChildAt(i);//获取子view
                if (i < 3) {//第一行
                    canvas.drawLine(0, 0,0, 0, localPaint);
                }
                if (i % column == 0) {//第一列
                    // canvas.drawLine(cellView.getLeft(), cellView.getTop(), cellView.getLeft(), cellView.getBottom(), localPaint);
                }
                if ((i + 1) % column == 0) {//第三列
                    //画子view底部横线
                    canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
                    //canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
                } else if ((i + 1) >=(childCount - (childCount % column))) {//如果view是最后一行
                    //画子view的右边竖线
                    canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
                    // canvas.drawLine(cellView.getLeft(), cellView.getBottom(), cellView.getRight(), cellView.getBottom(), localPaint);
                }else if(i==childCount){
                    canvas.drawLine(cellView.getRight(), cellView.getTop(), cellView.getRight(), cellView.getBottom(), localPaint);
                }else {//如果view不是最后一行

                    //画子view的右边竖线
                    canvas.drawLine(cellView.getRight(),0, cellView.getRight(), cellView.getBottom()+ cellView.getTop(), localPaint);
                    //画子view的底部横线
                    canvas.drawLine(0, cellView.getBottom(), cellView.getRight()+cellView.getLeft(), cellView.getBottom(), localPaint);
                }
            }
        }

    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = heightMeasureSpec;
        if (hasScrollBar) {
            expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                    MeasureSpec.AT_MOST);
            super.onMeasure(widthMeasureSpec, expandSpec);
        } else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        // TODO Auto-generated method stub
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }
}
